<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body></body>
</html>
<script>
  const obj1 = {
    firstName: '老',
    lastName: '王',
  };

  // Object.defineProperty给对象去添加或者修改某个属性，并且可以给属性设置描述
  Object.defineProperty(obj1, 'fullName', {
    // getter属性
    get() {
      return this.firstName + ' ' + this.lastName;
    },

    // setter属性
    set(val) {
      const [firstName, lastName] = val.split(' ');
      this.firstName = firstName;
      this.lastName = lastName;
    },
  });

  console.log(obj1.fullName); //当前存取器属性被获取的时候，可以调用getter方法
  obj1.fullName = 'xiao wang'; //当存取器属性被设置的时候，可以调用setter方法
  console.log(obj1.fullName);
</script>
